package savet.gui.modelos;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import savet.entidades.atributos.Legenda;

/**
 *
 * @author Adisson Marinho
 */
public class LegendaSelectionTableModel extends AbstractTableModel {

    private List<Legenda> legendas = new LinkedList<Legenda>();
    private List<Boolean> selecionadas = new LinkedList<Boolean>();

    public void addLegenda(Legenda legenda) {
        if (!legendas.contains(legenda)) {
            int indice = legendas.size();
            legendas.add(legenda);
            selecionadas.add(Boolean.FALSE);
            fireTableRowsInserted(indice, indice);
        }
    }

    public void removeLegenda(Legenda legenda) {
        if (legendas.contains(legenda)) {
            int index = legendas.indexOf(legenda);
            legendas.remove(legenda);
            selecionadas.remove(index);
            fireTableRowsDeleted(index, index);
        }
    }

    public int getRowCount() {
        return legendas.size();
    }

    public int getColumnCount() {
        return 2;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return columnIndex == 0 ? Boolean.class : Legenda.class;
    }

    @Override
    public String getColumnName(int column) {
        return column == 0 ? "" : "Legendas";
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return columnIndex == 0 ? selecionadas.get(rowIndex)
                : legendas.get(rowIndex);
    }

    @Override
    public void setValueAt(Object newValue, int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            System.out.println("changing model " + rowIndex + " with value " + newValue);
            selecionadas.set(rowIndex, (Boolean) newValue);
            fireTableCellUpdated(rowIndex, columnIndex);
        }
    }

    public void selecionarLegenda(Legenda legenda) {
        try {
            if (!legendas.contains(legenda)) {
                addLegenda(legenda);
            }
            selecionadas.set(legendas.indexOf(legenda), Boolean.TRUE);
        } catch (IndexOutOfBoundsException e) {
            Logger.getLogger(getClass().getName()).severe(e.getMessage());
        }
    }

    /**
     * Retorna a lista de legendas selecionadas no {@link JList}
     * @param index
     */
    public boolean isLegendaSelecionada(int index) {
        try {
            return selecionadas.get(index);
        } catch (IndexOutOfBoundsException e) {
            return false;
        }
    }

    /**
     *
     * @return legendas selecionadas
     */
    public List<Legenda> getLegendasSelecionadas() {
        // legendas selecionadas
        List<Legenda> ls = new LinkedList<Legenda>();
        for (int i = 0; i < selecionadas.size(); i++) {
            if (selecionadas.get(i)) {
                ls.add(legendas.get(i));
            }
        }
        return ls;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    @Override
    public void fireTableCellUpdated(int row, int column) {
        TableModelEvent event = new TableModelEvent(this, row, row, column, TableModelEvent.UPDATE);
        fireTableChanged(event);
    }
}
